package com.ellecity06.cwhiskymall.ui.mine.newer

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.QuickAdapterHelper
import com.chad.library.adapter.base.loadState.LoadState
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.databinding.LayoutEmptyViewBinding
import com.ellecity06.common.ktx.finishRefresh
import com.ellecity06.common.ktx.launchActivity
import com.ellecity06.common.ktx.observeEvent
import com.ellecity06.common.ktx.stringResToString
import com.ellecity06.common.utils.CommonUtils
import com.ellecity06.common.utils.ToastUtils
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.adapter.NewerLearningAdapter
import com.ellecity06.cwhiskymall.base.vm.PageIntent
import com.ellecity06.cwhiskymall.base.vm.PageState
import com.ellecity06.cwhiskymall.bean.response.NewerLearningResponse
import com.ellecity06.cwhiskymall.databinding.ActivityNewerLearningBinding
import com.ellecity06.cwhiskymall.helper.ShareHelper
import com.ellecity06.cwhiskymall.ui.video.AliPlayVideoActivity
import com.ellecity06.cwhiskymall.view.dialog.ShareDialog
import com.ellecity06.cwhiskymall.view.dialog.showDownLoad

/**
 * @author ellecity06
 * @time 2024/12/24 17:01
 * @des 新人教学
 */
class NewerLearningActivity : BaseMvvmActivity<ActivityNewerLearningBinding, NewerLearningViewModel>(),
    SwipeRefreshLayout.OnRefreshListener {
    private val adapter: NewerLearningAdapter by lazy { NewerLearningAdapter() }
    private var adapterHelper: QuickAdapterHelper? = null
    private val emptyViewBinding by lazy {
        LayoutEmptyViewBinding.inflate(
            layoutInflater,
            binding.rv,
            false
        )
    }


    override fun inflateBinding(): ActivityNewerLearningBinding {
        return ActivityNewerLearningBinding.inflate(layoutInflater)
    }

    override fun initialize(savedInstanceState: Bundle?) {
        title = R.string.title_newer_learing.stringResToString()
        initView()
        onRefresh()
        collectPageState()

    }

    private fun collectPageState() {

        viewModel.pageState.observeEvent(this) {
            when (it) {
                PageState.LoadError -> {
                    binding.refreshLayout?.finishRefresh()
                }

                is PageState.LoadMoreSuccess -> {
                    adapter.addAll(it.list as List<NewerLearningResponse>)
                    adapterHelper?.trailingLoadState = LoadState.NotLoading(!it.hasMore)
                }

                PageState.NoMore -> {
                    binding.refreshLayout.finishRefresh()
                    adapterHelper?.trailingLoadState = LoadState.None
                }

                is PageState.RefreshSuccess -> {
                    binding.refreshLayout.finishRefresh()
                    adapter.submitList(it.list as List<NewerLearningResponse>)
                    if (it.hasMore) {
                        adapterHelper?.trailingLoadState = LoadState.NotLoading(false)
                    } else {
                        adapterHelper?.trailingLoadState = LoadState.None
                    }
                }
                is PageState.RefreshError -> {
                    binding.refreshLayout.finishRefresh()
                    ToastUtils.show(it.msg)
                }
            }
        }
    }

    private fun initView() {
        binding.rv.layoutManager = LinearLayoutManager(this)
        binding.rv.adapter = adapter
        adapter.emptyView = emptyViewBinding.root
        adapter.isEmptyViewEnable = true
        binding.rv.adapter = adapter
        adapter.addOnItemChildClickListener(R.id.iv_play, ::onItemChildClick)
        adapter.addOnItemChildClickListener(R.id.layoutDownload, ::onItemChildClick)
        adapter.addOnItemChildClickListener(R.id.layoutCopy, ::onItemChildClick)
        adapter.addOnItemChildClickListener(R.id.layoutShare, ::onItemChildClick)
        binding.refreshLayout.setOnRefreshListener(this)
    }

    private fun onItemChildClick(baseQuickAdapter: BaseQuickAdapter<NewerLearningResponse, *>, view: View, i: Int) {
        val item = adapter.getItem(i)
        when (view.id) {
            R.id.iv_play -> {
                launchActivity<AliPlayVideoActivity>(Pair("video_url", item?.videoUrl))
            }

            R.id.layoutDownload -> {
                showDownLoad(item?.videoUrl ?: "")
            }

            R.id.layoutCopy -> {
                CommonUtils.copyStr(this@NewerLearningActivity, "${item?.title} ${item?.videoUrl}")
                ToastUtils.show(R.string.text_copy_success.stringResToString())
            }

            R.id.layoutShare -> {
                ShareDialog(this, object : ShareDialog.OnShareCallBack() {
                    override fun onCopy() {
                        CommonUtils.copyStr(this@NewerLearningActivity, "${item?.videoUrl}")
                        ToastUtils.show(R.string.text_copy_success.stringResToString())
                    }

                    override fun onWechat() {
                        ShareHelper.shareVideoToWx(
                            this@NewerLearningActivity,
                            item?.title ?: "",
                            item?.imageUrl ?: "",
                            item?.videoUrl ?: "",
                            item?.description ?: "",
                        ) {}
                    }

                    override fun onFriends() {
                        ShareHelper.shareVideoToWx(
                            this@NewerLearningActivity,
                            item?.title ?: "",
                            item?.imageUrl ?: "",
                            item?.videoUrl ?: "",
                            item?.description ?: "",
                            1
                        ) {}
                    }
                }).show()

            }
        }
    }

    override fun onRefresh() {
        lifecycleScope.launchWhenCreated {
            viewModel.pageIntent.send(PageIntent.Refresh)
        }
    }

}